package com.logistics.qrcode.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.map.entity.model.MapComponentsSave;
import com.logistics.qrcode.entity.domain.Qrcode;
import com.logistics.qrcode.mapper.QrcodeMapper;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.tool.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class QrcodeService extends ServiceImpl<QrcodeMapper, Qrcode> {

    /**
     * value by code
     * @param code
     * @param type
     * @return
     * @throws MessageException
     */
    public String valueByCode(String code, String type) {
        Qrcode result = this.getOne(new LambdaQueryWrapper<Qrcode>().eq(Qrcode::getCode, code).
                eq(Qrcode::getType, type));
        if(StringUtils.isNull(result)){
            return null;
        }
        return result.getValue();
    }

    /**
     * 查找qrcode信息
     * @param code
     * @param type
     * @return
     */
    public Qrcode queryByCode(String code, String type) {
        Qrcode result = this.getOne(new LambdaQueryWrapper<Qrcode>().eq(Qrcode::getCode, code).
                eq(Qrcode::getType, type));
        if(StringUtils.isNull(result)){
            return null;
        }
        return result;
    }

    /**
     * code by value
     * @param value
     * @param type
     * @return
     */
    public String codeByValue(String value, String type){
        Qrcode result = this.getOne(new LambdaQueryWrapper<Qrcode>().eq(Qrcode::getValue, value).
                eq(Qrcode::getType, type));
        if(StringUtils.isNull(result)){
           return null;
        }
        return result.getCode();
    }

    /**
     * 根据编码查找区域
     * @param code
     * @param type
     * @return
     */
    public String areaByCode(String code, String type) {
        Qrcode result = this.getOne(new LambdaQueryWrapper<Qrcode>().eq(Qrcode::getCode, code).
                eq(Qrcode::getType, type));
        if(StringUtils.isNull(result)){
            return null;
        }
        return result.getArea();
    }

    /**
     * code by values
     * @param values
     * @param type
     * @return
     */
    public List<Qrcode> codeByValues(List<String> values, String type){
        return this.list(new LambdaQueryWrapper<Qrcode>().in(Qrcode::getValue, values).
                eq(Qrcode::getType, type));
    }

    /**
     * 保存
     * @param save
     */
    public void save(MapComponentsSave save){
        this.save(Qrcode.build(save));
    }

    /**
     * 根据code删除数据
     * @param codes
     */
    public void deleteByCodes(List<String> codes) {
        this.remove(new LambdaQueryWrapper<Qrcode>().in(Qrcode::getCode, codes));
    }

    /**
     * 根据站点名查询数据
     * @param label
     * @return
     */
    public Qrcode findByCode(String label) {
        return this.getOne(new LambdaQueryWrapper<Qrcode>().eq(Qrcode :: getCode, label));
    }

}
